using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace FederationMembershipDB
{
	public class MedicalHistory
	{
        int _ID;
		bool AilmentsChecklist;
		string Notes;
		bool MedicalAuthorisation;

        public int ID
        {
            get { return _ID; }
        }

		public void AddMedicalHistory(bool ailments, string notes, bool medical)
		{
            AilmentsChecklist = ailments;
            Notes = notes;
            MedicalAuthorisation = medical;

            char c_AilmentsChecklist;
            char c_MedicalAuthorisation;

            if (AilmentsChecklist)
                c_AilmentsChecklist = 'T';
            else
                c_AilmentsChecklist = 'F';

            if (MedicalAuthorisation)
                c_MedicalAuthorisation = 'T';
            else
                c_MedicalAuthorisation = 'F';


            //Add to database
            Utilities.openDatabase();

            SqlCommand getNextID = new SqlCommand(string.Format("SELECT ISNULL(MAX(ID) + 1, 1) FROM MedicalHistory"), Utilities.connection);

            _ID = (int)(getNextID.ExecuteScalar());

            SqlCommand insertMedicalHistory =  new SqlCommand(string.Format("INSERT INTO MedicalHistory VALUES({0}, '{1}', '{2}', '{3}')",
                                                                             _ID, c_AilmentsChecklist, Notes, c_MedicalAuthorisation), Utilities.connection);
            insertMedicalHistory.ExecuteNonQuery();
            Utilities.connection.Close();
		}

		public string[] ViewMedicalHistory(int id)
		{
            string[] ret = new string[3];
            _ID = id;

            //retreive member from database
            Utilities.openDatabase();

            SqlCommand getdata = new SqlCommand(string.Format("SELECT med.AilmentsChecklist, med.MedicalAuthorisation, med.Notes FROM MedicalHistory med, Member m WHERE m.ID = {0} AND med.ID = m.MedicalHistory_ID", _ID), Utilities.connection);
            SqlDataReader dr1 = getdata.ExecuteReader();

            if (dr1.HasRows)
            {
                dr1.Read();
                for (int i = 0; i < 3; i++)
                {
                    ret[i] = (string)dr1[i];
                }

                Utilities.connection.Close();
                return ret;
            }
            else
            {
                Utilities.connection.Close();
                return null;
            }
		}

        public void UpdateMedicalHistory(int id, bool ailments, string notes, bool medical)
		{
            _ID = id;
            AilmentsChecklist = ailments;
            Notes = notes;
            MedicalAuthorisation = medical;

            char c_AilmentsChecklist;
            char c_MedicalAuthorisation;

            if (AilmentsChecklist)
                c_AilmentsChecklist = 'T';
            else
                c_AilmentsChecklist = 'F';

            if (MedicalAuthorisation)
                c_MedicalAuthorisation = 'T';
            else
                c_MedicalAuthorisation = 'F';

            Utilities.openDatabase();

            SqlCommand updateMedicalHistory = new SqlCommand(string.Format("UPDATE MedicalHistory SET AilmentsChecklist = '{0}', Notes = '{1}', MedicalAuthorisation = '{2}' WHERE id IN (SELECT m.MedicalHistory_ID FROM member m WHERE m.id = {3})",
                                                                 c_AilmentsChecklist, Notes, c_MedicalAuthorisation, _ID), Utilities.connection);
            updateMedicalHistory.ExecuteNonQuery();

            Utilities.connection.Close();

		}
	}
}
